home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / JActionButton.java < prev    next >
Text File  |  1998-10-07  |  5KB  |  169 lines

  1. package com.symantec.itools.swing.actions;
  2.  
  3. import java.beans.*;
  4. import com.sun.java.swing.*;
  5.  
  6. public class JActionButton
  7.     extends JButton
  8. {
  9.     public static final int DISPLAY_ACTION_ICON          = 1;
  10.     public static final int DISPLAY_ACTION_NAME          = 2;
  11.     public static final int DISPLAY_ACTION_ICON_AND_NAME = 3;
  12.     
  13.     public JActionButton()
  14.     {
  15.         setHorizontalTextPosition(RIGHT);
  16.         setVerticalTextPosition(CENTER);
  17.     }
  18.     
  19.     public JActionButton(com.sun.java.swing.Action newAction)
  20.     {
  21.         this();
  22.         setAction(newAction);
  23.     }
  24.     
  25.     //
  26.     // Properties
  27.     //
  28.     
  29.     // Action
  30.     
  31.     public com.sun.java.swing.Action getAction()
  32.     {
  33.         return m_Action;
  34.     }
  35.     
  36.     public void setAction(com.sun.java.swing.Action newAction)
  37.     {
  38.         //Handle bad arg
  39.         if (newAction == null)
  40.             throw new IllegalArgumentException();
  41.         
  42.         //Do nothing if no change
  43.         if (newAction == m_Action)
  44.             return;
  45.         
  46.         //Release last action
  47.         if (m_Action != null)
  48.         {
  49.             removeActionListener(m_Action);
  50.             m_Action.removePropertyChangeListener(m_ActionPropertyChangeListener);
  51.         }
  52.         
  53.         m_Action = newAction;
  54.         
  55.         addActionListener(m_Action);
  56.         
  57.         m_ActionPropertyChangeListener = createActionChangeListener();
  58.         m_Action.addPropertyChangeListener(m_ActionPropertyChangeListener);
  59.         
  60.         updateMenuItem();
  61.     }
  62.     
  63.     // Display
  64.     
  65.     public int getDisplay()
  66.     {
  67.         return m_Display;
  68.     }
  69.     
  70.     public void setDisplay(int newDisplay)
  71.     {
  72.         if (m_Display != newDisplay)
  73.         {
  74.             m_Display = newDisplay;
  75.             
  76.             updateMenuItem();
  77.         }
  78.     }
  79.     
  80.     //
  81.     // Implementation
  82.     //
  83.     
  84.     protected PropertyChangeListener createActionChangeListener()
  85.     {
  86.         return new ActionChangedListener();
  87.     }
  88.     
  89.     protected class ActionChangedListener
  90.         implements PropertyChangeListener
  91.     {
  92.         public void propertyChange(PropertyChangeEvent e)
  93.         {
  94.             String propertyName = e.getPropertyName();
  95.             if (propertyName.equals(Action.NAME))
  96.             {
  97.                 String text = (String) e.getNewValue();
  98.                 setText(text);
  99.             }
  100.             else if (propertyName.equals("enabled"))
  101.             {
  102.                 Boolean enabledState = (Boolean) e.getNewValue();
  103.                 setEnabled(enabledState.booleanValue());
  104.             }
  105.             else if (propertyName.equals(Action.SMALL_ICON))
  106.             {
  107.                 Icon icon = (Icon) e.getNewValue();
  108.                 setIcon(icon);
  109.                 invalidate();
  110.                 repaint();
  111.             } 
  112.         }
  113.     }
  114.     
  115.     public void setIcon(Icon newIcon)
  116.     {
  117.         if (newIcon == null)
  118.         {
  119.             super.setIcon(null);
  120.             setDisabledIcon(null);
  121.         }
  122.         else
  123.         {
  124.             super.setIcon(newIcon);
  125.             if (!(newIcon instanceof com.sun.java.swing.ImageIcon))
  126.             {
  127.                 if (newIcon instanceof com.symantec.itools.swing.icons.ImageIcon)
  128.                 {
  129.                     setDisabledIcon
  130.                         (new com.sun.java.swing.ImageIcon
  131.                             (GrayFilter.createDisabledImage
  132.                                 (((com.symantec.itools.swing.icons.ImageIcon)newIcon).getImage())));
  133.                 }
  134.             }
  135.         }
  136.     }
  137.     
  138.     protected void updateMenuItem()
  139.     {
  140.         if (m_Action == null)
  141.         {
  142.             setText("");
  143.             setIcon(null);
  144.             setEnabled(true);
  145.         }
  146.         else
  147.         {
  148.             if (m_Display == DISPLAY_ACTION_ICON_AND_NAME || m_Display == DISPLAY_ACTION_NAME)
  149.                 setText((String)m_Action.getValue(Action.NAME));
  150.             else
  151.                 setText("");
  152.             
  153.             if (m_Display == DISPLAY_ACTION_ICON_AND_NAME || m_Display == DISPLAY_ACTION_ICON)
  154.                 setIcon((Icon)m_Action.getValue(Action.SMALL_ICON));
  155.             else
  156.                 setIcon(null);
  157.             
  158.             setEnabled(m_Action.isEnabled());
  159.         }
  160.         
  161.         invalidate();
  162.         repaint();
  163.     }
  164.     
  165.     protected com.sun.java.swing.Action m_Action = null;
  166.     protected PropertyChangeListener    m_ActionPropertyChangeListener = null;
  167.     protected int                       m_Display = DISPLAY_ACTION_ICON_AND_NAME;
  168. }
  169.